<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="订单编号" prop="orderNumber">
                      <el-input v-model="formData.orderNumber" placeholder="请输入订单编号" />
                    </el-form-item>
                    <el-form-item label="食品名称" prop="productName">
                      <el-input v-model="formData.productName" placeholder="请输入食品名称" />
                    </el-form-item>
                    <el-form-item label="食品规格" prop="productSpecification">
                      <el-input v-model="formData.productSpecification" placeholder="请输入食品规格" />
                    </el-form-item>
                    <el-form-item label="食品数量" prop="quantity">
                      <el-input v-model="formData.quantity" placeholder="请输入食品数量" />
                    </el-form-item>
                    <el-form-item label="付款价格" prop="price">
                      <el-input v-model="formData.price" placeholder="请输入付款价格" />
                    </el-form-item>
                    <el-form-item label="订单状态，例如：待支付、已支付、已取消" prop="orderStatus">
                      <el-radio-group v-model="formData.orderStatus">
                            <el-radio label="1">请选择字典生成</el-radio>
                      </el-radio-group>
                    </el-form-item>
                    <el-form-item label="购物车id" prop="cartId">
                      <el-input v-model="formData.cartId" placeholder="请输入购物车id" />
                    </el-form-item>
                    <el-form-item label="食品id" prop="productId">
                      <el-input v-model="formData.productId" placeholder="请输入食品id" />
                    </el-form-item>
                    <el-form-item label="商品图片">
                      <ImageUpload v-model="formData.productImage"/>
                    </el-form-item>
                    <el-form-item label="订单+食品id 为了做区分" prop="productCartCode">
                      <el-input v-model="formData.productCartCode" placeholder="请输入订单+食品id 为了做区分" />
                    </el-form-item>
                    <el-form-item label="用户id" prop="userId">
                      <el-input v-model="formData.userId" placeholder="请输入用户id" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as OrdersApi from '@/api/system/orders';
  import ImageUpload from '@/components/ImageUpload';
      export default {
    name: "OrdersForm",
    components: {
          ImageUpload,
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            orderNumber: undefined,
                            productName: undefined,
                            productSpecification: undefined,
                            quantity: undefined,
                            price: undefined,
                            orderStatus: undefined,
                            cartId: undefined,
                            productId: undefined,
                            productImage: undefined,
                            productCartCode: undefined,
                            userId: undefined,
        },
        // 表单校验
        formRules: {
                        orderNumber: [{ required: true, message: '订单编号不能为空', trigger: 'blur' }],
                        productName: [{ required: true, message: '食品名称不能为空', trigger: 'blur' }],
                        quantity: [{ required: true, message: '食品数量不能为空', trigger: 'blur' }],
                        price: [{ required: true, message: '付款价格不能为空', trigger: 'blur' }],
                        orderStatus: [{ required: true, message: '订单状态，例如：待支付、已支付、已取消不能为空', trigger: 'blur' }],
                        productId: [{ required: true, message: '食品id不能为空', trigger: 'blur' }],
                        userId: [{ required: true, message: '用户id不能为空', trigger: 'blur' }],
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await OrdersApi.getOrders(id);
            this.formData = res.data;
            this.title = "修改订单";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增订单";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await OrdersApi.updateOrders(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await OrdersApi.createOrders(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            orderNumber: undefined,
                            productName: undefined,
                            productSpecification: undefined,
                            quantity: undefined,
                            price: undefined,
                            orderStatus: undefined,
                            cartId: undefined,
                            productId: undefined,
                            productImage: undefined,
                            productCartCode: undefined,
                            userId: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>